package com.yksj.healthtalk.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.yksj.healthtalk.comm.RootFragment;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.JsonsfHttpResponseHandler;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.dictionary.DictionaryMainActivity;
import com.yksj.healthtalk.ui.friend.FriendMainUI;
import com.yksj.healthtalk.ui.interestwall.InterestMainActivity;
import com.yksj.healthtalk.ui.salon.SalonMainUIActivity;
import com.yksj.healthtalk.ui.views.CirclePageIndicator;
import com.yksj.healthtalk.utils.SharePreUtils;
/**
 * 
 * @author jack_tang
 *	病友社区
 */
public class PatientsCommunityFragment extends RootFragment implements OnClickListener {

	private View view;
	private TabsPagerAdapter adapter;
	private Context context;
	

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.patients_community_fragment_layout, null);
		initUI(view);
		return view;
	}
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		context=activity;
	}
	private void initUI(View view){
		ViewPager mViewPager = (ViewPager)view.findViewById(R.id.pager);
		CirclePageIndicator indicator = (CirclePageIndicator)view.findViewById(R.id.indicator);
		adapter = new TabsPagerAdapter();
		mViewPager.setAdapter(adapter);
		indicator.setViewPager(mViewPager);
	}
	
	private class TabsPagerAdapter extends PagerAdapter{
		final List<View> views = new ArrayList<View>();
		View v1;
		public TextView picReadNumber;//分享所见右上角未读消息
		public TabsPagerAdapter() {
			LayoutInflater layoutInflater =LayoutInflater.from(context);
			v1 = layoutInflater.inflate(R.layout.patients_community_ui_fragment_one,null);
			v1.findViewById(R.id.health_services).setOnClickListener(PatientsCommunityFragment.this);
			v1.findViewById(R.id.salon_talk).setOnClickListener(PatientsCommunityFragment.this);
			v1.findViewById(R.id.personal_publishing).setOnClickListener(PatientsCommunityFragment.this);
			v1.findViewById(R.id.medical_knowledge).setOnClickListener(PatientsCommunityFragment.this);
			picReadNumber=(TextView) v1.findViewById(R.id.pic_number_text);
			views.add(v1);
		}

		@Override
		public int getItemPosition(Object object) {
			return super.getItemPosition(object);
		}
		
		@Override
		public int getCount() {
			return views.size();
		}
		
		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			View item = views.get(position);
			container.addView(item);
			return item;
		}
		
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View)object);
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}
	}

	@Override
	public void onClick(View v) {
		Intent intent;
		switch (v.getId()) {
		case R.id.title_back:
			onBackPressed(v);
			break;
		case R.id.health_services://病友寻找
			intent = new Intent(context, FriendMainUI.class);
			startActivity(intent);
			break;
		case R.id.salon_talk://话题讨论
			intent = new Intent(context, SalonMainUIActivity.class);
			startActivity(intent);
			break;
		case R.id.personal_publishing://社区发布
			intent = new Intent(context, InterestMainActivity.class);
			startActivity(intent);
			break;
		case R.id.medical_knowledge://医学百科
			 intent = new Intent(context,DictionaryMainActivity.class);
			 startActivity(intent);
			break;
		}
		
	}
	private void queryDataCount(){
		HttpRestClient.doHttpMyZoneCount("20",SmartFoxClient.getLoginUserId(),SharePreUtils.fatchNewsReadedLastId(),SharePreUtils.fatchInterestWallId(),new JsonsfHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, JSONObject response) {
				super.onSuccess(statusCode, response);//新闻：80
				try{
					meSageCount(adapter.picReadNumber, response.getInteger("200"));
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		});
	}
	//更新未读消息条数界面
	private void meSageCount(TextView id,int count){
		if(count!=0){
			id.setText(count >100 ? "99+":count+"");
			id.setVisibility(View.VISIBLE);
		}else{
			id.setVisibility(View.GONE);
		}
	}
	@Override
	public void onResume() {
		super.onResume();
			queryDataCount();
		
	}
}
